import { useUserStore } from '../stores/user'
import request from '../utils/request'
import type { BaseResponse } from '../types/api-response'
import type { ArticleItem } from '../types/article'

// 获取文章列表
export const getArticleList = () => {
    const userStore = useUserStore()
    console.log(userStore.token)
    return request<BaseResponse<ArticleItem[]>>({
        url: '/article/mlist',
        method: 'GET',
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'token': `${userStore.token}`
        }
    })
}

// 新增文章详情接口
export const getArticleDetail = (id: number) => {
    const userStore = useUserStore()
    return request<BaseResponse<ArticleItem>>({
        url: '/article/detail',
        method: 'GET',
        headers: {
            'token': `${userStore.token}`
        },
        params: { id }
    })
}

// 新增互动接口
export const interactApi = {
    // 喜爱度统计
    addLoveStatistic: (articleId: number) => {
        const userStore = useUserStore()
        return request({
            url: '/statistics/addlove',
            method: 'POST',
            headers: { 'token': userStore.token },
            params: { articleId }
        })
    },

    // 点赞
    like: (id: number) => {
        const userStore = useUserStore()
        return request({
            url: '/interact/like',
            method: 'POST',
            headers: { 'token': userStore.token },
            params: { id }
        })
    },

    // 取消点赞
    unlike: (id: number) => {
        const userStore = useUserStore()
        return request({
            url: '/interact/unlike',
            method: 'GET',
            headers: { 'token': userStore.token },
            params: { id }
        })
    },

    // 收藏
    collect: (id: number) => {
        const userStore = useUserStore()
        return request({
            url: '/interact/collect',
            method: 'POST',
            headers: { 'token': userStore.token },
            params: { id }
        })
    },

    // 取消收藏
    uncollect: (id: number) => {
        const userStore = useUserStore()
        return request({
            url: '/interact/uncollect',
            method: 'DELETE',
            headers: { 'token': userStore.token },
            params: { id }
        })
    }
}

interface Article {
    title: string;
    categoryId?: number;
    coverImg?: string;
    content: string;
    state: string;
}

// 添加文章
export const articleAdd = (article: Article) => {
    const userStore = useUserStore();
    console.log(userStore.token)
    return request<BaseResponse>({
        url: "/article/add",
        method: 'post',
        headers: {
            'token': `${userStore.token}`
        },
        data: article
    })
}